package net.wesley.android.city;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import android.os.Handler;
import android.os.Message;

import net.wesley.android.City;
import net.yebaihe.sdk.HttpConnection;

public class Yantai extends City {

	private Handler request2handler=new Handler(){
		public void handleMessage(Message message){
			switch (message.what){
			case HttpConnection.DID_ERROR://DID ERROR
				callback.onComplete(-1, "");
				break;
			case HttpConnection.DID_SUCCEED://DID SUCC
				String body=(String)message.obj;
				int index=body.indexOf("是否已交款</td>");
				if (index>0){
					String str=body.substring(index).split("</table>")[0];
					index=str.indexOf("font-weight:700;\" colspan=\"3\">");
					if (index>0){
						callback.onComplete(-10001, "ret:"+str.substring(index+30).split("</td>")[0]);
						return;
					}
					
					String[] rows=str.split("<tr>");
					String retvalue="";
					for (int i=1;i<rows.length;i++){
						String[] cols=rows[i].split("<td>");
						String shijian=cols[1].split("</td>")[0];
						String didian=cols[2].split("</td>")[0];
						String code=cols[3].split("</td>")[0];
						retvalue+=String.format("shijian:%s\ndidian:%s\ncode:%s\n\n",shijian,didian,code);
					}
					callback.onComplete(rows.length-1, retvalue);
					return;
				}
				else{
					callback.onComplete(0, "");
				}
				break;
			}
		}
	};

	public void requestParamsFinish(String[] values) {
		try {
			new HttpConnection(ctx,request2handler,"GBK").post("http://cx.ytjj.gov.cn:9081/ytjjvio/seekfxc.jsp", 
					String.format("cartype=02&vin2=&vin=%s&hphm=%s&image.x=7&image.y=8",
							values[0],URLEncoder.encode(chepai,"GBK")));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean match(String chepai) {
		return chepai.startsWith("鲁F"); 
	}

	@Override
	public String[] paramDesc() {
		String[] s={"车架号后4位"};
		return s;
	}

}
